home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / pov / gen / treebas / treebas.txt < prev    next >
Text File  |  1993-05-15  |  14KB  |  291 lines

  1.  
  2.  
  3. I would ike to warn the reader right now that I am a very poor programer,  I
  4. am sure that there are much easier and faster ways to achieve the objectives
  5. of this program than the way I have done it.  I learned to program in basic
  6. as I was writing this program.  The program itself was written in qbasic which
  7. came with my version of Ms-Dos 5.00.  I had to rely heavily on sequential files
  8. to get this program to work because of the 128k enviorment limit on my version
  9. of qbasic.
  10.  
  11.         I will assume that the reader of this document is famillar with
  12. l-systems in my explaniation of the inner workings of the program.   I will
  13. begin with the 'turtle' interpitation.
  14.  
  15.         The sub called turtle handles the drawing of the l-system.  It
  16.    responds to the following commands.  The program keeps track of the
  17.    turtles position in 3-space and also keeps track of the three vectors
  18.    H, L , and U.  which represent the turtles heading, the direction to the
  19.    Left of the turtle, and the direction up from the turtle respectivly.  The
  20.    turtle responds to the following commands:
  21.  
  22.         F(x) : move forward and draw a line x units in the direction of H
  23.  
  24.         f(x) : move forward without drawing a line x units in the direction
  25.                of H
  26.  
  27.         +(x) : rotate the turtle by x (in radians) about the U vector.
  28.                x can be positive or negative depending on the direction
  29.                wanted.
  30.  
  31.         &(x) : rotate the turtle by x (in radians) about the L vector.
  32.                x can be positive or negative.
  33.  
  34.         /(x) : rotate the turtle by x (in radians) about the H vector.
  35.                x can be positive or negative.
  36.  
  37.  
  38.  
  39.          [   : stack the current position of the turtle along with the
  40.                orientation of the turtle.  Also stacks current color, line
  41.                length,  line width, and just about everything else that
  42.                effects the drawing of the object.
  43.  
  44.          ]   :  pull from the stack and make current the orientation, position,
  45.                 color, width, length, and everything else that effects the
  46.                 drawing of the object.
  47.  
  48.          !(x):  change the width of the lines be drawn to x.
  49.  
  50.          $   :  roll the turtle so that the L vector fufills the following
  51.                 equation:
  52.                                      V X H
  53.                                 L = -------
  54.                                     |V X H|
  55.  
  56.                 then recalulate U so that U = H X L
  57.  
  58.                 V is the vector pointing opposite to that of gravity.
  59.  
  60.  
  61.         --------------------------------------------------------------------
  62.  
  63.            The turtle orientation is also effected by a tropisim vector T.
  64.         After every line segment is drawn with F(x) the H vector is rotated
  65.         slightly in the direction of T.
  66.  
  67.            This rotates the turtle about the vector H X T.  the angle of
  68.            rotation is determined by getting the angle alpha between H and T
  69.            and then rotating the turtle e*alpha degrees.  e is a constant
  70.            that represents how much T effects the turtle.
  71.  
  72.         -----------------------------------------------------------------------
  73.  
  74.         < text > :  transfer control to the polygon generator and pass the
  75.                     text within the brackets to the polygon generator.
  76.                     when the polygon generator is finished executing
  77.                     the commands stored in the text it restores the
  78.                     turtle information as it was before the call.
  79.  
  80.                 the polygon generator responds to the following commands
  81.  
  82.                         { : push the current polygon onto the top of the
  83.                             polygon stack and start a new polygon.
  84.                         * : append the new vertex to the current polygon
  85.                             the coordienits of which are determined by the
  86.                             current position of the turtle.
  87.                         } :  Draw the current polygon using the supplied
  88.                             vertices,  then pop the top polygon from the
  89.                             polygon stack.
  90.                       F(x):  Move forward x units and use the result
  91.                              as the next vetex
  92.                       G(x):  Move forward, do not create new vertex
  93.  
  94.  
  95.         This is not yet completly implemented....
  96.  
  97.         ______________________________________________________________________
  98.  
  99.         Everything else in the file being read is ignored.
  100.  
  101.  
  102.       I assume that the reader is famillar with the way that the productions
  103.       work in l-systems.  In this program it is handled by a sub called
  104.       'productions'.  This does the replacements of the symbols [,],{,},and G
  105.       along with a few other commands  that do not have parameters assioated with
  106.       them.  When it comes to a word that has parameters then it passes control
  107.       to the sub 'handler' which then passes control to the appropriate sub
  108.       that replaces it according to the rules within that sub.
  109.  
  110.         for example if the program hit A(1,10) then it would pass control
  111.         to the sub 'handler' which would in turn pass control to the sub A.
  112.         look at sub A to see how the production works.
  113.  
  114.      I had to write my own subs that converted between numeric and string type
  115.      variables for the parameters since the way qbasics built in conversion
  116.      routines occasionaly produced strings that had charicters that the program
  117.      uses to seperate the numbers.  For example, if I used the built in numeric
  118.      to string conversion it would sometimes produce a string that had a comma
  119.      or ) symbol both of which would cause the getparam sub to think that it
  120.      was done reading the number in the case of a comma or done reading the
  121.      parameters in the case of ).  I could remedy this problem by using
  122.      a random access file type but qbasics online help wasn't very helpful
  123.      in describing the proper use of fields in conjunction with a random access
  124.      file.
  125.  
  126.  
  127.         Heres a list of the subs and what they do:
  128.  
  129.                 poly : not currently being used
  130.                 polygon : reads the file 'points.raw' that is produced
  131.                           by the branchman sub, anb creates a raw triangle
  132.                           file for programs such as raw2pov
  133.                           This sub usually produces output that works
  134.                           fine with raw2pov.  Unfortunatly when the file
  135.                           size gets large raw2pov aborts with the error
  136.                           'unexpected end of file' on the last line number
  137.                           of the file generated.  I have not found a reason for
  138.                           this.
  139.  
  140.                branchman : This sub is called from the turtle sub after every
  141.                            F(x) command.  It stack the H and L vectors
  142.                            and creates  points on a octogon by rotating L
  143.                            around H  45 degrees 8 times and recording the
  144.                            values in 'points.raw'.
  145.  
  146.                tropisim :  This sub is called after every F(x).  This has
  147.                            alredy been explained above.  If you do not want
  148.                            this effect on the object make T()=0
  149.  
  150.                stack(stype$):  this sub controls the stacking operation
  151.                                 for the [ and ] commands.  the array stac()
  152.                                 is the stack it controls.
  153.  
  154.                 rotateX:  does the rotation for the $ command.
  155.                 widthman:  This sub is called from the turtle sub
  156.                             when the !(x) command is encountered.  sets the variable
  157.                             wid to x.  wid is used by branchman as a scalar
  158.                             against L to control the width of the branches.
  159.  
  160.                rotateU:  rotates the turtle about the U vector.
  161.                rotateL:  rotates the turtle about the L vector.
  162.                rotateH:  rotates the turtle about the H vector.
  163.  
  164.                trans3d2d:  translates the 3d co-ords to 2d co-ords for viewing
  165.                            while turtle draws the initial shape.
  166.  
  167.                drawline:  draws a line between the old location of the
  168.                           turtle to the new location.  called by movef.
  169.  
  170.                movef :  called by turtle when F(x) or f(x) is encountered
  171.                         in the file being read.  If F(x) was passed then
  172.                         drawline is called.  if f(x) then drawline is not called
  173.  
  174.                number2string(anumber) : converts anumber to number$
  175.  
  176.  
  177.                getparams : called by a production sub.  returns all of the
  178.                            parameters in the way of an array called numberarray.
  179.                            the first valuse in numberarray is the number of
  180.                            parameters in the word being looked at.
  181.                            ie if A(10,1) were encountered by the productions
  182.                            sub control would be passed to the sub A which
  183.                            would in turn call getparams to get the
  184.                            parameters assioated with A.  getparams would then
  185.                            return numberarray which would look like:
  186.                                 numberarray(1,1) = 2  ; since there are
  187.                                                         2 pararmeters
  188.                                 numberarray(1,2) = 10 ; value of
  189.                                                         first parameter
  190.  
  191.                                 numberarray(1,2) = 1 ; value of 2nd parameter
  192.  
  193.                            this sub also calls string2num to convert the
  194.                            string format to a numeric variable.
  195.  
  196.                 string2num: (ntemp$) converts ntemp$ to a numeric variable
  197.                             atemp.
  198.  
  199.                 productions:  reads the file Axiom and produces a file
  200.                                called 'commands.raw' for use by the turtle
  201.                                sub.
  202.  
  203.                 convst: used by num2string
  204.  
  205.                 handler : used by productions to sort out what it is doing and
  206.                           and where to pass control to.
  207.  
  208.                 f : production sub for the command F(x)
  209.                 sf : production sub for the command f(x)
  210.                 A : production sub for the command A(x,y)
  211.                 B : production sub for the command B(x,y)
  212.                 cC: production sub for the command C(x,y)
  213.                 D : production sub for the command D(x1,x2,...,xn)
  214.                 RU: production sub for one of the rotate commands
  215.                 RL :'                                           '
  216.                 RH : '                                          '
  217.                 decr: production sub for the !(x) command
  218.                 colour: not yet implemented,  but will be a production sub
  219.                         for '(x) where x is the new color to draw in.
  220.  
  221.                 powercon : used by string2num
  222.  
  223.  
  224.  
  225.   initial variables:
  226.                         scale :  the scale at which the
  227.                                  shape is drawn on the screen
  228.                                     _       _       _        _
  229.                         V(1)-V(3):  V = V(1)i + V(2)j  + V(3)k
  230.                         h(1)-h(3):  the inital heading vector H
  231.                                     _   _       _       _
  232.                                     H = h(1)i + h(2)j + h(3)k
  233.  
  234.                         l(1)-l(3): same as above but for the L vector
  235.                         u(1)-u(3): same as above but for the u vector
  236.                         phi: the viewing angle for the shape as it is
  237.                                 drawn on string from the positive z-axis
  238.  
  239.                         th: the viewing angle measured from the x-axis
  240.                         a0 : branching angle from trunk
  241.                         a2 : branching angle from lateral axes
  242.                         r1 : contraction ratio for the trunk
  243.                         r2 : contraction ratio for the branches
  244.                         ds : divergence angle
  245.                         wr : width decrease rate
  246.                         e  : the effect of T on the turtle (tropisim)
  247.                         T(1)-T(3):  the T vector for tropisim calculations
  248.                                 _       _     _     _
  249.                                 T = T(1)i+T(2)j+T(3)k
  250.  
  251.  
  252.  
  253.                 I almost forgot.  This program uses  axes orientated in the
  254.                 following way :
  255.                                                 |z
  256.                                                 |     /
  257.                                                 |   /
  258.                                                 | /
  259.                                   --------------+--------------
  260.                                               / |             y
  261.                                             /   |
  262.                                           / x   |
  263.                                                 |
  264.  
  265.   I hope that this poor description helps whomever is reading this.
  266.  
  267.   If you make any changes that speed this program up or add new functions
  268.   I would be interested in seeing the code.  You can contact me via The Graphics
  269.   Alternative BBS as Ed Smith.
  270.  
  271.         P.S.  I guess I am placing this code in the public domain.  Use it
  272.               as you see fit.  I got most of my ideas for the program for a book
  273.               called 'The Algorithmic Beauty of plants.' It's a good one.
  274.  
  275.  
  276.  
  277.  
  278.  
  279.                                                         Ed Storm
  280.                                                         (209)474-9159
  281.                                                         May 11, 1993
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.